// 平台兼容API适配层
let platformAPI;

// 检测运行环境并初始化对应API
function initPlatformAPI() {
    // 检测Electron环境
    if (window.electronAPI) {
        // Electron环境：直接使用主进程暴露的API
        platformAPI = {
            // 提醒相关
            showAlert: (data) => window.electronAPI.showAlert(data),
            closeAlert: (id) => window.electronAPI.closeAlert(id),
            onAlertClosed: (callback) => window.electronAPI.onAlertClosed(callback),

            // 窗口控制
            minimizeWindow: () => window.electronAPI.minimizeWindow(),
            maximizeWindow: () => window.electronAPI.maximizeWindow(),
            closeWindow: () => window.electronAPI.closeWindow(),

            // 心跳相关
            sendHeartbeat: () => window.electronAPI.sendHeartbeat(),
            onHeartbeatStatus: (callback) => window.electronAPI.onHeartbeatStatus(callback)
        };
    } else {
        // 移动端/Web环境：实现浏览器兼容版本的API
        platformAPI = {
            // 提醒相关（移动端用浏览器通知或自定义弹窗）
            showAlert: async (data) => {
                // 尝试使用浏览器通知
                if (Notification.permission === 'granted') {
                    new Notification(data.title, { body: data.message });
                } else if (Notification.permission !== 'denied') {
                    const permission = await Notification.requestPermission();
                    if (permission === 'granted') {
                        new Notification(data.title, { body: data.message });
                    }
                }
                // 同时显示页面内弹窗（可调用Vue组件）
                const event = new CustomEvent('show-alert', { detail: data });
                window.dispatchEvent(event);
            },

            closeAlert: (id) => {
                console.log('移动端关闭提醒', id);
                const event = new CustomEvent('close-alert', { detail: id });
                window.dispatchEvent(event);
            },

            onAlertClosed: (callback) => {
                window.addEventListener('alert-closed', (e) => callback(e.detail));
            },

            // 移动端没有窗口控制，空实现或提示
            minimizeWindow: () => console.log('移动端不支持窗口最小化'),
            maximizeWindow: () => console.log('移动端不支持窗口最大化'),
            closeWindow: () => console.log('移动端不支持窗口关闭'),

            // 心跳相关（移动端直接通过WebSocket实现）
            sendHeartbeat: async () => {
                try {
                    // 假设已在别处初始化了WebSocket连接
                    if (window.appWebSocket && window.appWebSocket.readyState === WebSocket.OPEN) {
                        window.appWebSocket.send(JSON.stringify({
                            type: 'heartbeat',
                            timestamp: Date.now()
                        }));
                    }
                } catch (error) {
                    console.error('发送心跳失败:', error);
                }
            },

            onHeartbeatStatus: (callback) => {
                window.addEventListener('heartbeat-status', (e) => callback(e.detail));
            }
        };
    }

    return platformAPI;
}

// 初始化并导出单例
const api = initPlatformAPI();
export default api;
